package com.common.config;

import com.common.domain.R;
import com.common.utils.JwtUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.common.holder.UserHolder;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.IOException;

/**
 * @description:
 * @Author: Xhy
 * @CreateTime: 2023-10-25 20:17
 */
@Component
public class AdminInterceptor implements HandlerInterceptor {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("WebMVC执行乐乐乐乐乐乐乐乐"+request.getHeader("userId"));
        if (request.getMethod().equals("OPTIONS")){
            return true;
        }

        if (!JwtUtils.checkToken(request)) {
            System.out.println("TOKEN校验失败咯");
            return false;
        }

        UserHolder.setUserIdHodler(request.getHeader("userId"));
        return true;
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //移除用户
        UserHolder.removeUserIdHolder();
    }
}
